package com.heima.article.listen;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.common.constants.message.NewsUpOrDownConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class ArticleUpOrDownListener {
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    //文章上架接受消息修改表中的值
    @RabbitListener(queuesToDeclare = {
            @Queue(value = NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE)
    })
    public void newsUpHandler(String articleId){
        log.info("接收到文章上架消息");
        //获取消息队列中的信息 对config表进行更新
        apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                        .set(ApArticleConfig::getIsDown,false)
                .eq(ApArticleConfig::getArticleId,articleId));

    }
    //文章下架
    @RabbitListener(queuesToDeclare = {
            @Queue(value = NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE)
    })
    public void newsDownHandler(String articleId){
        log.info("接收到文章下架消息");
        //修改文章config下架
        apArticleConfigService.update(
                Wrappers.<ApArticleConfig>lambdaUpdate()
                        .set(ApArticleConfig::getIsDown,true)
                        .eq(ApArticleConfig::getArticleId,articleId)
        );

    }
}
